﻿using TMPro;
using UnityEngine;
using Yoozoo.UI.YGUI;
using com.yoozoo.gta;
using System;

namespace Yoozoo.Gameplay.RTS
{
    public class SmugglerHudLod0 : MonoBehaviour
    {
        public bool HpBarVisible;
        public GameObject HpBarObj;
        public SpriteRenderer hpBar;
        public TMP_Text hpBarTxt;
        public TMP_Text levelTxt;
        public TMP_Text timeTxt;
        public GameObject Logo;
        public SpriteRenderer timeBg;
        public SpriteRenderer itemIcon;

        public GameObject worldPosTip;
        public TMP_Text worldPosTxt;
        public WorldPosTipData worldPosTipData;

        public void SetItemIcon(string icon)
        {
            //if (itemIcon != null)
            //{
            //    ArmyManager.Instance.ResourceManager.SetAtlasIcon(icon, itemIcon);
            //}
        }
        public void SetLevel(int level)
        {
            if (levelTxt != null)
            {
                levelTxt.text = level.ToString();
            }
        }
        public void SetLogoActive(bool active)
        {
            Logo.SetActive(active);
        }
        public void SetHpActive(bool active)
        {
            HpBarVisible = active;
            HpBarObj.gameObject.SetActive(active);
        }

        public void SetHp(int currentHp,int maxHp)
        {
            double result = (double)currentHp / maxHp; // 将其中一个操作数转换为 double 类型，以执行浮点数除法
            double percentage = result * 100; // 将结果乘以 100，得到百分比值
            string formattedPercentage = percentage.ToString("0.0"); // 格式化百分比值，保留一位小数
            hpBarTxt.text = formattedPercentage + "%";
            hpBar.material.SetFloat("_Amount", 1 - (float)result);
        }
        
        private Vector3 hpOriginPosition;
        private Vector3 levelOriginPosition;
        
        public void SetLocalPosition(Vector3 position)
        {
            if (hpOriginPosition == default)
            {
                hpOriginPosition = HpBarObj.transform.localPosition;
            }

            if (levelOriginPosition == default)
            {
                levelOriginPosition = Logo.transform.localPosition;
            }
            transform.localPosition = position;
            var scale = transform.localScale.y;
            HpBarObj.transform.localPosition = hpOriginPosition / scale;
            
            Logo.transform.localPosition = levelOriginPosition / scale;
        }

        public void UpdateSmugglerWorldPosTip(Int64 troopId, bool bShow,string tip = "")
        {
            if (worldPosTip)
            {
                worldPosTip.SetActive(bShow);
            }
            if (worldPosTipData)
            {
                worldPosTipData.type = 1;
                worldPosTipData.uid = troopId;
            }
            if (worldPosTxt)
            {
                worldPosTxt.text = tip;
            }
        }
    }
}
